var app = getApp();
Page({
  data: {
    selectNum: false,
    selectTime: false,
    selectTimeText: '7',
    selectNumText: '吨',
    imgUrl: [], //显示图片
    img: [], //存储新名字图片
    selectNumId: "",
<<<<<<< .mine

=======
    mo: false
>>>>>>> .theirs
  },
  //下拉单位
  selectNumClick: function() {
    this.setData({
      selectNum: !this.data.selectNum
    })
  },
  //下拉单位选择
  mySelectNum: function(e) {
    console.log(e);
    this.setData({
      selectNumText: e.target.dataset.name,
      selectNum: !this.data.selectNum,
      selectNumId: e.currentTarget.dataset.unitid
    })
  },
  //下拉时间
  selectTimeClick: function() {
    this.setData({
      selectTime: !this.data.selectTime
    })
  },
  //下拉时间选择
  mySelectTime: function(e) {
    this.setData({
      selectTimeText: e.target.dataset.nametime,
      selectTime: !this.data.selectTime
    })
  },

  // 添加图片
  addPhoto: function() {
    var that = this;
    var openid = wx.getStorageSync('openid');
    if (this.data.imgUrl.length == 5) {
      wx.showToast({
        title: '最多只能上传5张图片',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    } else {
      wx.chooseImage({
        count: 1,
<<<<<<< .mine
        success: function(res) {
          var tempFilePaths = res.tempFilePaths[0];
          that.data.imgUrl.push(res.tempFilePaths[0]);
          that.setData({
            imgUrl: that.data.imgUrl
          })
          wx.uploadFile({
            url: 'https://www.mamingwang.cn/api/adds/based',
            filePath: tempFilePaths,
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              mid: that.data.mid ? that.data.mid : "",
              openid: openid,
              type: 1
            },
            success(res) {
              console.log(res)
              var mid = JSON.parse(res.data);
              that.data.img.push(mid.data[1])
              that.setData({
                mid: mid.data[0],
                img: that.data.img
              })
            }
          })
=======
        success: function (res) {
          var tempFilePaths = res.tempFilePaths[0];
          that.data.imgUrl.push(res.tempFilePaths[0]);
          that.setData({
            imgUrl: that.data.imgUrl
          })
          wx.uploadFile({
            url: 'https://www.mamingwang.cn/api/adds/based',
            filePath: tempFilePaths,
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              mid: that.data.mid ? that.data.mid : "",
              openid: openid,
              type: 1
            },
            success(res) {
              console.log(res)
              var mid = JSON.parse(res.data);
              that.data.img.push(mid.data[1])
              that.setData({
                mid: mid.data[0],
                img: that.data.img
              })
            }
          })
>>>>>>> .theirs
        },
      })
    }
  },
  //删除图片
  delPhoto: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    var openid = wx.getStorageSync("openid");
    var that = this;
    this.data.imgUrl.splice(e.target.id, 1);
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/del',
      data: {
        openid: openid,
        mid: this.data.mid,
        photo: this.data.img[e.target.id]
      },
      method: "post",
      success(res) {
        console.log(res)
        that.data.img.splice(e.target.id, 1);
        that.setData({
          imgUrl: that.data.imgUrl,
          img: that.data.img
        })
      }
    })
  },
  //提交
<<<<<<< .mine
  formsubmit: function(e) {

=======

  formsubmit: function (e) {
>>>>>>> .theirs
    var openid = wx.getStorageSync("openid");
    var that = this;
    var city = wx.getStorageSync('city');
    console.log(city)
    if (!e.detail.value.title) {
      wx.showToast({
        title: '标题不能为空',
        icon: "none",
        duration: 1500,
        mask:true
      })
<<<<<<< .mine

=======

>>>>>>> .theirs
    } else if (e.detail.value.title.length > 12) {
      wx.showToast({
        title: '标题不能超过12个字',
        icon: "none",
        duration: 1500,
        mask:true
      })
    } else if (!e.detail.value.xiangxi) {
      wx.showToast({
        title: '详细内容不能为空',
        icon: "none",
        duration: 1500,
        mask:true
      })
    } else if (!e.detail.value.number) {
      wx.showToast({
        title: '数量不能为空',
        icon: "none",
        duration: 1500,
        mask:true
      })
    } else {
      wx.showToast({
        title: '请稍后',
        icon: 'loading',
        duration: 1000,
        mask: true,
      })
      wx.request({
        url: 'https://www.mamingwang.cn/api/adds/adds',
        data: {
          openid: openid,
          title: e.detail.value.title,
          content: e.detail.value.xiangxi,
          mid: this.data.mid,
          number: e.detail.value.number,
          price: e.detail.value.price,
          unit: this.data.selectNumText,
          timelimit: this.data.selectTimeText,
          address: city ? city : ""
        },
        method: "post",
        success(res) {
          console.log(res)
<<<<<<< .mine
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 1500,
            mask: true,
            success(){
              setTimeout(function(){
                wx.switchTab({
                  url: '../fabu',
                })
              },1500)
            }
          })























=======
          if (res.data.code == 1) {
            wx.showToast({
              title: '发布成功',
              icon: "none",
              duration: 1500,
            })

            setTimeout(function () {
              wx.switchTab({
                url: '../fabu',
              })
            }, 1500)
            setTimeout(function () {
              wx.switchTab({
                url: '../fabu',
              })
            }, 1500)
          } else if (res.data.code == 22) {
            wx.showToast({
              title: '今日发布次数已达上限',
              icon: "none",
              duration: 1500
            })
          } else if (res.data.code == 12) {
            wx.showToast({
              title: '今日发布次数已达上限',
              icon: "none",
              duration: 1500
            })
          } else if (res.data.code == 0) {
            wx.showToast({
              title: '您无权发布',
              icon: "none",
              duration: 1500
            })
          }
>>>>>>> .theirs
        }
      })
    }
  },
<<<<<<< .mine
  onUnload: function() {

=======
  removecatch: function () { },
  onUnload: function () {
>>>>>>> .theirs
    var openid = wx.getStorageSync('openid');
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/delsql',
      data: {
        openid: openid,
        mid: this.data.mid ? this.data.mid : ""
      },
      method: "post",
      success(res) {
        console.log(res)
      }
    })
  },
})